package com.dc.aimc.lab.controller;

import com.dc.aimc.common.service.response.JsonResponse;
import com.dc.aimc.lab.dto.FileDTO;
import com.dc.aimc.lab.entity.ArticleFile;
import com.dc.aimc.lab.entity.MinIOBucket;
import com.dc.aimc.lab.entity.MinIOFile;
import com.dc.aimc.lab.util.MinIOUtil;
import io.minio.messages.Bucket;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Log4j2
@RestController
@RequestMapping("minio")
@Api(tags = "内容管理")
public class MinIOController {

    @Autowired
    private MinIOUtil minIOUtil;


    @ApiOperation(value = "创建桶")
    @PostMapping("bucket/{bucketName}")
    public JsonResponse<Object> createBucket(@PathVariable("bucketName") String bucketName) {
        minIOUtil.createBucket(bucketName);
        return JsonResponse.success();
    }

    @ApiOperation(value = "删除桶")
    @DeleteMapping("bucket/{bucketName}")
    public JsonResponse<Object> deleteBucket(@PathVariable("bucketName") String bucketName) {
        boolean isRemove = minIOUtil.removeBucket(bucketName);
        return isRemove ? JsonResponse.success() : JsonResponse.fail();
    }

    @ApiOperation(value = "桶信息列表")
    @GetMapping("/bucket/list")
    public JsonResponse<Object> listBuckets() {
        List<MinIOBucket> buckets = minIOUtil.listBuckets();
        return JsonResponse.success(buckets);
    }


    @ApiOperation(value = "上传文件")
    @PostMapping("object")
    public JsonResponse<Object> uploadFile(ArticleFile file) {
        try {
            MultipartFile[] files = file.getFiles();
            for (MultipartFile multipartFile : files) {
                minIOUtil.uploadFile(file.getBucket(), file.getPath() + multipartFile.getOriginalFilename(), multipartFile.getInputStream());
            }
            return JsonResponse.success();
        } catch (IOException e) {
            log.error("文件上传异常: ", e);
        }
        return JsonResponse.fail();
    }

    @ApiOperation(value = "获取文件下载地址")
    @PostMapping("object/url/dto")
    public JsonResponse<Object> fileUrl(@RequestBody FileDTO dto) {
        String url = minIOUtil.getFileUrl(dto.getBucketName(), dto.getFileName());
        return JsonResponse.success(url);
    }

    @ApiOperation(value = "下载文件")
    @PostMapping("object/dto")
    public void downloadFile(@RequestBody FileDTO dto, HttpServletResponse response) {
        minIOUtil.downLoadFile(dto.getBucketName(), dto.getFileName(), response);
    }

    @ApiOperation(value = "文件信息列表")
    @PostMapping("object/list/dto")
    public JsonResponse<Object> listFiles(@RequestBody FileDTO dto) {
        List<MinIOFile> results = minIOUtil.listFiles(dto.getBucketName(), dto.getFolderName());
        return JsonResponse.success(results);
    }

}
